<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <!-- <view class="jtop_l" @tap="navigateBack">关闭</view> -->
      <view class="jtop_c">新增支出明细</view>
      <view
        class="jtop_r"
        :style="{ opacity: payment.payedAmount ? '1' : '.6' }"
        @click="jsave"
        >确定</view
      >
    </view>
    <view class="tophr"></view>

    <view class="addb">
      <view class="addi2">
        <text>材料ID</text>
        <input
          type="text"
          placeholder="请输入材料ID"
          :value="payment.materialId"
          @input="onKeyInput_payment_materialId"
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <view class="addi2">
        <text>支付名目</text>
        <input
          type="text"
          placeholder="请输入支付名目"
          :value="payment.name"
          @input="onKeyInput_payment_name"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="addi2">
        <text>支付方式</text>
        <input
          type="text"
          placeholder="请输入支付方式"
          :value="payment.payMethod"
          @input="onKeyInput_payment_payMethod"
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <e-picker-plus
        endRule="2040-12-31 24:59"
        class="addbi"
        @confirm="confirm"
        mode="YMDhm"
      >
        <text class="addbil">支付时间</text>
        <image src="../../static/img/jt.png" @confirm="confirm"></image>
        <text class="addbir">{{ payment.payTime }}</text>
      </e-picker-plus>

      <view class="addi2">
        <text>已付金额</text>
        <input
          type="text"
          placeholder="请输入已付金额"
          :value="payment.payedAmount"
          @input="onKeyInput_payment_payedAmount"
          placeholder-style="color:#CCCCCC"
        />
      </view>

      <view class="addi2">
        <text>收款人</text>
        <input
          type="text"
          placeholder="请输入收款人"
          :value="payment.payee"
          @input="onKeyInput_payment_payee"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="addi2 noborder">
        <text>备注</text>
        <input
          type="text"
          placeholder="请输入备注"
          :value="payment.remark"
          @input="onKeyInput_payment_remark"
          placeholder-style="color:#CCCCCC"
        />
      </view>
    </view>

    <view class="addi">
      <text>支付凭证</text>
      <view class="att">
        <view v-for="(item, index) in jimg" :key="item.id">
          <image class="at" :src="item" @longpress="logoTime(index)"></image>
        </view>

        <image
          src="../../static/img/index/new/at.png"
          mode=""
          class="at"
          @tap="changeAvatar()"
        ></image>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState } from 'vuex';

export default {
  data() {
    return {
      dw: '消耗品',
      index: '1',
      jsex2: [
        { value: '1', label: '消耗品' },
        { value: '2', label: '机具' },
      ],
      repeat_click: true,
      ashowpop: false,
      ashowpop_jrename: false,
      ashowpop_jdel: false,
      ashowpopT: false,
      bj: false,
      yd: false,
      lastInTime: '',
      remark: '',
      amount: '',
      model: '',
      brand: '',
      name: '',
      index: 0,
      array: [],
      array2: [],
      // total_price: '0.00',

      batchList: [
        {
          amount: '',
          materialId: '',
          price: '',
          remark: '',
          specification: '',
          unit: '',
          total_price: '0.00',
        },
      ],
      material: {
        category: '',
        name: '',
        type: 1,
        unit: '',
      },
      payment: {
        materialId: '',
        name: '',
        payMethod: '',
        payTime: '',
        payedAmount: '',
        payee: '',
        remark: '',
        voucher: '',
      },

      jimg: [],

      category: '',
      name: '',
      unit: '',
      jc: uni.getStorageSync('jc'),
      // array: ['中国', '美国', '巴西', '日本'],
    };
  },

  methods: {
    //图片上传
    btTC_jdel() {
      var ax = this.jimg;
      ax.splice(this.scdtp, 1);
      console.log('ax1111', ax);
      this.jimg = ax;
      console.log('ax222', this.jimg);
      this.ashowpop2 = false;
    },

    btTCC(e) {
      this.ashowpop2 = !this.ashowpop2;
    },

    logoTime(e) {
      console.log(e);
      this.scdtp = e;
      this.ashowpop2 = true;
    },

    changeAvatar() {
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera', 'album'], //从相册选择、摄像头
        success: function(res) {
          console.log(JSON.stringify(res.tempFilePaths, res.tempFiles[0]));
          that.sctp(res.tempFilePaths, res.tempFiles[0]);
        },
      });
    },

    sctp(a, b) {
      console.log(a);
      var als = '';
      var tpbt = b.name;
      if (tpbt == undefined || tpbt == 'undefined') {
        tpbt = b.path;
      }
      var pos = tpbt.lastIndexOf('.');
      var suffix = tpbt.substring(pos);
      function random_string(len) {
        len = len || 32;
        var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
          pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
      }
      var g_object_name = random_string(10) + suffix;

      // 获取信息
      request({
        url: '/biz/oss/policy',
        method: 'GET',

        success: res => {
          if (res.data.success == true) {
            als = res.data.data;
            request({
              filePath: a[0],
              oss: true,
              formData: {
                name: tpbt,
                key: als.dir + g_object_name,
                policy: als.policy,
                OSSAccessKeyId: als.accessid,
                signature: als.signature,
                callback: als.callback,
                success_action_status: '200',
              },

              success: res => {
                var cc = JSON.parse(res.data);
                console.log('上传图片', cc);
                if (cc.code == '00000') {
                  var dd = this.jimg;
                  dd.push(cc.data.url + '/' + als.dir + g_object_name);
                  this.jimg = dd;
                } else {
                  this.repeat_click = true;
                  uni.showToast({
                    title: cc.message,
                    icon: 'none',
                  });
                }
              },
            });
          }
        },
      });
    },

    //--图片上传

    bindPickerChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.target.value);
      this.material.type = e.target.value + 1;
      this.dw = this.jsex2[e.target.value].label;
      console.log('type', this.material.type);
    },

    add_jbox() {
      this.batchList.push({
        amount: '',
        materialId: '',
        price: '',
        remark: '',
        specification: '',
        unit: '',
        total_price: '0.00',
      });
      console.log('1212', this.batchList);
    },

    confirm(e, a) {
      console.log(e, a);
      this.payment.payTime = e.result;
    },

    onKeyInput_amount: function(event, id) {
      this.batchList[id].amount = event.detail.value;
      if (this.batchList[id].amount && this.batchList[id].price) {
        this.batchList[id].total_price =
          parseFloat(this.batchList[id].amount) *
          parseFloat(this.batchList[id].price);
      } else {
        this.batchList[id].total_price = '0.00';
      }
    },

    onKeyInput_param: function(event, id) {
      this.batchList[id].param = event.detail.value;
    },

    onKeyInput_price: function(event, id) {
      this.batchList[id].price = event.detail.value;
      if (this.batchList[id].amount && this.batchList[id].price) {
        this.batchList[id].total_price =
          parseFloat(this.batchList[id].amount) *
          parseFloat(this.batchList[id].price);
      } else {
        this.batchList[id].total_price = '0.00';
      }
    },

    onKeyInput_specification: function(event, id) {
      this.batchList[id].specification = event.detail.value;
    },

    onKeyInput_remark: function(event, id) {
      this.batchList[id].remark = event.detail.value;
    },
    onKeyInput_materialId: function(event, id) {
      this.batchList[id].materialId = event.detail.value;
    },
    onKeyInput_unit: function(event, id) {
      this.material.unit = event.detail.value;
    },

    onKeyInput_bunit: function(event, id) {
      this.batchList[id].unit = event.detail.value;
    },

    onKeyInput_category: function(event, id) {
      this.material.category = event.detail.value;
    },

    onKeyInput_name: function(event, id) {
      this.material.name = event.detail.value;
    },

    onKeyInput_payment_materialId: function(event, id) {
      this.payment.materialId = event.detail.value;
    },
    onKeyInput_payment_name: function(event, id) {
      this.payment.name = event.detail.value;
    },
    onKeyInput_payment_payMethod: function(event, id) {
      this.payment.payMethod = event.detail.value;
    },
    onKeyInput_payment_payedAmount: function(event, id) {
      this.payment.payedAmount = event.detail.value;
    },
    onKeyInput_payment_payee: function(event, id) {
      this.payment.payee = event.detail.value;
    },
    onKeyInput_payment_remark: function(event, id) {
      this.payment.remark = event.detail.value;
    },

    jsave() {
      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;
      if (!this.payment.payMethod) {
        this.repeat_click = true;
        uni.showToast({
          title: '请输入支付方式',
          icon: 'none',
        });
        return;
      }
      // if(!this.brand){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入材料品牌',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.model){
      // 						this.repeat_click = true;
      //					uni.showToast({
      // 		title: '请输入型号/规格',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.amount){
      // 						this.repeat_click = true;
      //uni.showToast({
      // 		title: '请输入采购数量',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.remark){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入备注',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      // if(!this.lastInTime){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入进场时间',
      // 		icon: 'none'
      // 	})
      // 	return
      // }
      this.payment.voucher = this.jimg;
      request({
        url: `/biz/material-payment`,

        data: {
          // materialId:  this.payment.materialId,
          materialId: this.jc.id,
          name: this.payment.name,
          payMethod: this.payment.payMethod,
          payTime: this.payment.payTime,
          payedAmount: this.payment.payedAmount,
          payee: this.payment.payee,
          remark: this.payment.remark,
          voucher: this.payment.voucher,
        },

        method: 'POST',
        success: openIdRes => {
          console.log(openIdRes);
          if (openIdRes.data.success == true) {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1000,
              success: function() {
                setTimeout(function() {
                  uni.navigateBack();
                }, 1500);
              },
            });

            // uni.navigateBack()
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    // 跳转
    jshow_pop() {
      this.ashowpop = !this.ashowpop;
    },
    btTC2() {
      uni.navigateTo({
        url: '../main/work_add',
      });
    },

    navigateBack() {
      uni.navigateBack();
    },
  },
  computed: mapState(['forcedLogin', 'hasLogin', 'userName']),
  onLoad() {
    // request({
    //   url: `/modor/v1/common/uom`,
    //   data: {},
    //   method: 'GET',
    //   success: openIdRes => {
    //     if (openIdRes.data.success == true) {
    //       var ss = openIdRes.data.data;
    //       ss = JSON.stringify(ss);
    //       ss = ss.substr(1);
    //       ss = ss.substr(0, ss.length - 1);
    //       ss = ss.replace(/"/g, '');
    //       ss = ss.replace(/"/g, '');
    //       ss = ss.split(',');
    //       var arr = [];
    //       var arr2 = [];
    //       for (var i = 0; i < ss.length; i++) {
    //         var c2 = ss[i];
    //         var str_before = c2.split(':')[0];
    //         var str_after = c2.split(':')[1];
    //         arr2.push({ value: str_before, label: str_after });
    //       }
    //       for (var i = 0; i < ss.length; i++) {
    //         var c2 = ss[i];
    //         var str_before = c2.split(':')[0];
    //         var str_after = c2.split(':')[1];
    //         arr.push(str_after);
    //       }
    //       this.array = arr;
    //       this.array2 = arr2;
    //       console.log(this.array);
    //       // uni.navigateBack()
    //     } else {
    //       this.repeat_click = true;
    //       uni.showToast({
    //         title: openIdRes.data.msg,
    //         icon: 'none',
    //       });
    //       this.isLoading = false;
    //     }
    //   },
    //   fail: () => {
    //     this.repeat_click = true;
    //     uni.showToast({
    //       title: '服务器开小差，请稍后~',
    //       icon: 'none',
    //     });
    //     return false;
    //   },
    // });
  },
};
</script>

<style>
.tophr {
  width: 100%;
  height: 88rpx;
}
.uni-searchbar {
  box-shadow: none;
}

.ptt {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.pttl {
  width: 84rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 20rpx 18rpx 30rpx;
}
.pttr {
  width: 400rpx;
  height: 84rpx;
  display: block;
  float: left;
  margin: 18rpx 0rpx 0rpx 0rpx;
}
.pttrt {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 34rpx;
  color: #000000;
}
.pttrf {
  width: 100%;
  height: 42rpx;
  line-height: 42rpx;
  display: block;
  float: left;
  font-size: 24rpx;
  color: #999999;
}
.pttjt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 48rpx 30rpx 0 30rpx;
}
.ptf {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.ptft {
  width: 750rpx;
  margin: 0 0 0 0rpx;
  line-height: 90rpx;
  text-align: center;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
}
.ptft image {
  width: 48rpx;
  height: 48rpx;
  display: inline-block;
  float: none;
  margin: 0 10rpx -10rpx 0;
}
.ptft text {
  width: auto;
  height: 48rpx;
  display: inline-block;
  float: none;
  font-size: 34rpx;
  line-height: 48rpx;
  color: #000000;
}

.addi2 {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addi {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  box-shadow: inset 0px 1px 0px #dddddd, inset 0px -1px 0px #dddddd;
  margin: 20rpx 0 0;
}
.addi2 text {
  width: 250rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 0rpx;
}
.addi text {
  width: 250rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
  margin: 0 0 0 30rpx;
}
.addi input,
.addi2 input,
.addi2 .qt {
  width: auto;
  height: 50rpx;
  display: block;
  float: left;
  margin: 20rpx 30rpx 0 0;
  text-align: left;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #000000;
}
.addb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  margin: 20rpx 0 0;
}
.addbi {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.addb .addbi:last-child {
  border: none;
}
.addbi .addbil {
  width: 250rpx;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 34rpx;
  color: #000000;
}
.addbi .addbir {
  width: auto;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  font-size: 32rpx;
  color: #000000;
}
.addbi image {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 33rpx 30rpx 0 30rpx;
}
.addi textarea {
  width: 690rpx;
  height: 150rpx;
  display: block;
  float: left;
  margin: 0rpx 30rpx 20rpx;
  text-align: left;
  line-height: 40rpx;
  font-size: 34rpx;
  color: #000000;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}
.jtop_r {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 20rpx 20rpx 0 0;
}
.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}
.doc_b22 {
  opacity: 0.5;
}

.nd {
  width: 735rpx;
  height: auto;
  display: block;
  float: left;
  margin: 0 0 0 0rpx;
}
.nd_bt {
  width: auto;
  height: 48rpx;
  display: block;
  float: right;
  border: 1px solid #dddddd;
  border-radius: 24rpx;
  margin: 40rpx 0 40rpx 30rpx;
}
.nd_bt text {
  width: auto;
  height: 48rpx;
  display: block;
  float: left;
  margin: 0 7rpx 0 18rpx;
  line-height: 48rpx;
  color: #666666;
  font-size: 24rpx;
}
.nd_bt image {
  width: 13rpx;
  height: 8rpx;
  display: block;
  float: left;
  margin: 21rpx 14rpx 0 0;
}

.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: block;
}
.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}
.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #f2f2f2;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}
.tc_fbox_bt2 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 0rpx solid #f2f2f2;
  background: #ffffff;
}
.tc_fbox_bt3 {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-bottom: 1px solid #dddddd;
  background: #ffffff;
}

.addi2 .qt {
  width: 50%;
}
.ub_jt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 32rpx 30rpx 0 0;
}

.addi21 {
  width: 50%;
  float: left;
}
.addi21 text {
  width: 100rpx;
}
.addi21 input {
  width: 250rpx;
  float: left;
  margin: 20rpx 0 0 0;
}
.ub_jt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 32rpx 30rpx 0 0;
}
.ub_t {
  width: 50%;
  height: 40rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  line-height: 40rpx;
  text-align: left;
  color: #000000;
  margin: 25rpx 20rpx 0 0;
}
.ub_t image {
  width: 36rpx;
  height: 36rpx;
  display: block;
  float: right;
  margin: 0rpx 0rpx 0 6rpx;
}
.ub_tx {
  width: 114rpx;
  height: 114rpx;
  display: block;
  float: right;
  margin: 30rpx 20rpx 30rpx 0;
  border-radius: 8rpx;
}
.att {
  width: 720rpx;
  margin: 0 0 30rpx 30rpx;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}
.at {
  width: 160rpx;
  height: 160rpx;
  display: block;
  float: left;
  margin: 0rpx 0 0 0;
}
</style>
